<?php

namespace App\Services\Index\Elasticsearch;

use App\Services\Service;
use App\Services\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Settlement\Info as SettlementInfo;
use App\Core\Traits\Numeral;

class SummaryService extends Service
{
    use DatabaseElasticsearch, Numeral;

    /**
     * 概要统计【游戏人数】
     */
    public function playerGame($request)
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【类型】
        $this->makeWhere($params, 'player_type', SettlementInfo::PLAYER_TYPE);
        // 筛选【平台】
        $this->makeWhereOrg($request, $params);

        $paramsSum = $params;
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $paramsSum, 'end_time');
        // 统计数据【数据合计】
        $paramsSum['body']['aggs']['distinct_uid'] = [
            'cardinality' => ['field' => 'uid', 'precision_threshold' => 40000]
        ];
        // 查询数据
        $results = SettlementInfo::search($paramsSum);
        // 数据合计
        $total = $results['aggregations']['distinct_uid']['value'];

        // 筛选【时间】
        $this->makeWhereLastDays($request, $params, 'end_time', 7);
        // 获取时间参数
        $datetime = $this->getParamArray($request, 'datetime');
        // 统计数据【数据列表】
        $params['body']['aggs']['group'] = [
            'date_histogram' => [
                'field' => 'end_time_at',
                'time_zone' => '+08:00',
                'calendar_interval' => '1d',
                'format' => 'yyyy-MM-dd',
                'min_doc_count' => 0,
                'extended_bounds' => [
                    'min' => $datetime[0] * 1000,
                    'max' => $datetime[1] * 1000,
                ],
            ],
            'aggs' => [
                'distinct_uid' => [
                    'cardinality' => [
                        'field' => 'uid',
                        'precision_threshold' => 40000
                    ]
                ],
            ],
        ];
        // 查询数据
        $results = SettlementInfo::search($params);
        // 制作数据
        $collection = collect(array_map(
            function ($item) {
                return [
                    'timed' => $item['key'] / 1000,
                    'amount' => $item['distinct_uid']['value'],
                ];
            },
            $results['aggregations']['group']['buckets'] ?? []
        ));

        return [
            $collection,
            $total
        ];
    }

    /**
     * 概要统计【平台收益】
     */
    public function amount($request)
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【类型】
        $this->makeWhere($params, 'player_type', SettlementInfo::PLAYER_TYPE);
        // 筛选【平台】
        $this->makeWhereOrg($request, $params);

        $paramsSum = $params;
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $paramsSum, 'end_time');
        // 统计数据【数据合计】
        $this->makeAgg($paramsSum, 'usd.gain_player', 'sum');
        // 查询数据
        $results = SettlementInfo::search($paramsSum);
        // 数据合计
        $total = -$results['aggregations']['usd.gain_player']['value'];

        // 筛选【时间】
        $this->makeWhereLastDays($request, $params, 'end_time', 7);
        // 获取时间参数
        $datetime = $this->getParamArray($request, 'datetime');
        // 统计数据【数据列表】
        $params['body']['aggs']['group'] = [
            'date_histogram' => [
                'field' => 'end_time_at',
                'time_zone' => '+08:00',
                'calendar_interval' => '1d',
                'format' => 'yyyy-MM-dd',
                'min_doc_count' => 0,
                'extended_bounds' => [
                    'min' => $datetime[0] * 1000,
                    'max' => $datetime[1] * 1000,
                ],
            ],
            'aggs' => [
                'usd.gain_player' => [
                    'sum' => [
                        'field' => 'usd.gain_player',
                    ]
                ],
            ],
        ];
        // 查询数据
        $results = SettlementInfo::search($params);
        // 制作数据
        $collection = collect(array_map(
            function ($item) {
                return [
                    'timed' => $item['key'] / 1000,
                    'amount' => $this->makeRound(-$item['usd.gain_player']['value']),
                ];
            },
            $results['aggregations']['group']['buckets'] ?? []
        ));

        return [
            $collection,
            $total
        ];
    }
}
